//
//  HMHomeOriginalView.swift
//  Weibo
//
//  Created by Apple on 2017/7/13.
//  Copyright © 2017年 KY. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
import YYText

//  原创微博视图
class HMHomeOriginalView: UIView {
    //  原创微博记录上次底部约束
    var originalBottomConstraint: Constraint?
    
    //  引用ViewModel
    var statusViewModel: HMStatusViewModel? {
        didSet {
            if let imageUrl = statusViewModel?.status?.user?.profile_image_url {
                userImageView.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            screenLabel.text = statusViewModel?.status?.user?.screen_name
//            contentLabel.text = statusViewModel?.status?.text
            contentLabel.attributedText = statusViewModel?.originalAttributedStr
            mbrankImageView.image = statusViewModel?.mbrankImage
            verifiedTypeImageView.image = statusViewModel?.verifiedTypeImage
            sourceLabel.text = statusViewModel?.sourceContent
            timeLabel.text = statusViewModel?.timeContent
            //  卸载上次约束
            originalBottomConstraint?.deactivate()
            
            //  判断是否有配图数据
            if let picUrls = statusViewModel?.status?.pic_urls, picUrls.count > 0 {
                //  有配图
                pictureView.isHidden = false
                self.snp.makeConstraints({ (make) in
                    originalBottomConstraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                })
                //  绑定数据
                pictureView.picUrls = picUrls
            } else {
                //  没有配图
                pictureView.isHidden = true
                //  重写创建底部约束
                self.snp.makeConstraints({ (make) in
                    originalBottomConstraint = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                })
                
            }
        }
    }
    
    //  头像
    private lazy var userImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //  认证类型等级
    private lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //  昵称
    private lazy var screenLabel: UILabel = {
        let label = UILabel(fontSize: 13, textColor: UIColor.darkGray)
        return label
    }()
    //  会员等级
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //  时间
    private lazy var timeLabel: UILabel = {
        let label = UILabel(fontSize: 12, textColor: UIColor.orange)
        return label
    }()
    //  来源
    private lazy var sourceLabel: UILabel = {
        let label = UILabel(fontSize: 12, textColor: UIColor.lightGray)
        return label
    }()
    //  微博内容
    private lazy var contentLabel: YYLabel = {
//        let label = UILabel(fontSize: 13, textColor: UIColor.darkGray)
        let label = YYLabel()
        //  最大换行宽度
        label.preferredMaxLayoutWidth = ScreenWidth - 2 * HomeCellMargin
        label.numberOfLines = 0
        return label
    }()
    //  转发微博配图
    private lazy var pictureView: HMHomePictureView = {
        let pictureView = HMHomePictureView()
        pictureView.backgroundColor = self.backgroundColor
        return pictureView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  添加控件设置约束
    private func setupUI() {
        backgroundColor = UIColor.white
        addSubview(userImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenLabel)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        screenLabel.text = "哈哈"
        timeLabel.text = "刚刚"
        sourceLabel.text = "来自: 微博"
        contentLabel.text = "水电费；加快了对方静安寺；考虑到附近是打飞机；啊世纪东方；了看见啊豆腐；了看见爱上；考虑到附近；拉世纪东方；了看见爱上；独立开发静安寺；了快递劫匪；奥斯卡京东方；了看见爱上的；父控件给控件；了空间撒地方；看了；可接受的；访客"
        
        userImageView.snp.makeConstraints { (make) in
            make.top.leading.equalTo(self).offset(HomeCellMargin)
            //  设置约束优先级
            make.size.equalTo(CGSize(width: 35, height: 35)).priority(.high)
        }
        verifiedTypeImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(userImageView.snp.trailing)
            make.centerY.equalTo(userImageView.snp.bottom)
        }
        screenLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userImageView)
            make.leading.equalTo(userImageView.snp.trailing).offset(HomeCellMargin)
        }
        mbrankImageView.snp.makeConstraints { (make) in
            make.top.equalTo(screenLabel)
            make.leading.equalTo(screenLabel.snp.trailing).offset(HomeCellMargin)
        }
        timeLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(userImageView)
            make.leading.equalTo(screenLabel)
        }
        sourceLabel.snp.makeConstraints { (make) in
            make.top.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp.trailing).offset(HomeCellMargin)
        }
        contentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userImageView.snp.bottom).offset(HomeCellMargin)
            make.leading.equalTo(userImageView)
            make.trailing.equalTo(self).offset(-HomeCellMargin)
        }
        pictureView.snp.makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp.bottom).offset(HomeCellMargin)
            make.leading.equalTo(contentLabel)
            //  设置约束优先级
            make.size.equalTo(CGSize(width: 100, height: 100)).priority(.high)
        }
        
        //  设置关键约束
        self.snp.makeConstraints { (make) in
            originalBottomConstraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
        }
        
        contentLabel.highlightTapAction = { (containerView, attributedText, range, rect) in
            let result = (attributedText.string as NSString).substring(with: range)
            print(result)
        }
        
    }

}
